<?php
require_once('icq.php');
header('Content-Type: text/plain');

// timeout is 10
// that means: on default, the client waits 10 seconds for every reply
// if unset, the default socket timeout is used
$icq = new icq_client(10);

// $uin refers to an ICQ number
// you can set your status on login by setting $status to another value
// (for example ICQ_STATUS_AWAY)
function login($uin, $password, $host = 'login.icq.com', $status = ICQ_STATUS_ONLINE) {
	global $icq;

	echo("Logging in...\n");
	$res = $icq->login($uin, $password, $host, $status);
	if(!$res) die("Could not login.\n");
}

// creating a new ICQ account works only once a day per IP address!!
function createUIN() {
	global $icq;

	echo("Creating new ICQ account...");
	$uin = $icq->create_uin('bbb');

	if(!$uin) echo("Could not create UIN\n");
	else echo "UIN $uin has been created\n";
}

// $uin refers to an ICQ number
function sendMessage($uin) {
	global $icq;

	echo "Sending test message to $uin...\n";
	$icq->send_msg_simple($uin, 'This is a test message. ICQ lib works!');
}


// receives any flap message
// read "http://www.micq.org/ICQ-OSCAR-Protocol-v7-v8-v9/" if you want
// to know what flaps and snacs are
function receiveAnything($wait = 10) {
	global $icq;

	echo("Waiting for any messages for the next $wait seconds...\n");
	$res = $icq->recv($flap);
	if(!$res) echo "Could not receive anything in $wait seconds.\n";
	else {
		echo "Received message:\n\n";
		var_dump($flap);
		echo "\n";
	}
}

function setMyGeneralProfile() {
	global $icq;

	// for information on what arguments to pass see icq.php line 1265 to 1278
	echo("Updating profile...\n");
	$icq->send_metasetgeneral('New-Nickname', 'John','Harrison', 'any@email.org', 'New York');
}

// $uin refers to an ICQ number
function getUserStatus($uin) {
	global $icq;

	echo("Trying to get a user's status...\n");
	// add user to contact list
	$icq->add_contact($uin);

	// listen for incoming messages for 4 seconds
	// if a status information is received, it will be passed to
	// the handler 'handle_srv_useronline' that is registered on default
	// handlers can be set using methods 'set_snac_handler' and
	// 'set_flap_handler'
	// read "http://www.micq.org/ICQ-OSCAR-Protocol-v7-v8-v9/" if you want
	// to know what flaps and snacs are
	$icq->listen(NULL, 4);

	// get status from the in-memory status information cache
	$status = $icq->get_status($uin);

	switch($status) {
		case ICQ_STATUS_ONLINE:
			echo "User $uin is online\n"; break;
		case ICQ_STATUS_OFFLINE:
			echo "User $uin is offline\n"; break;
		case ICQ_STATUS_AWAY:
			echo "User $uin is away\n"; break;
		case ICQ_STATUS_DND:
			echo "User $uin is in do-not-disturb mode\n"; break;
		default:
			echo "I know $uin's status, but I'm to lazy to look it up\n";
	}
}




// login with correct account here...

// login('99999999', 'password', 'login.icq.com', ICQ_STATUS_AWAY);



// call any of the above functions here...

// getUserStatus("82626595");
// sendMessage("82626595");
// receiveAnything();




echo("Closing ICQ connection\n");
$icq->close();
?>